//
//  AttributeCollectionView.m
//  天巢新1期
//
//  Created by 唐建平 on 15/12/15.
//  Copyright © 2015年 JP. All rights reserved.
//

#import "AttributeView.h"
#import "UIView+Extnesion.h"
#define margin 10
@interface AttributeView ()
@property (nonatomic ,weak) UIButton *btn;
@end

@implementation AttributeView

/**
 *  返回一个创建好的属性视图,并且带有标题.创建好之后必须设置视图的Y值.
 *
 *  @param texts 属性数组
 *
 *  @param viewWidth 视图宽度
 *
 *  @return attributeView
 */
+ (AttributeView *)attributeViewWithTitle:(NSString *)title titleFont:(UIFont *)font andTitleImageNamed:(NSString *)imageNamed andDetailTitle:(NSString *)detailTitle attributeTexts:(NSArray *)texts viewWidth:(CGFloat)viewWidth{
    int count = 0;
    float btnW = 0;
    AttributeView *view = [[AttributeView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *iv = [[UIImageView alloc] init];
    iv.frame= CGRectMake(15, (45-20)*0.5, 20, 20);
    iv.image = [UIImage imageNamed:imageNamed];
    [view addSubview:iv];
    
    
    UILabel *label = [[UILabel alloc]init];
    label.text = [NSString stringWithFormat:@"%@",title];
    label.font = font;
    label.textColor = [UIColor colorWithHexString:@"999999"];
    CGSize size = [label.text sizeWithFont:font];
//    label.frame = (CGRect){{iv.width +15+5,10},size};
    label.frame = CGRectMake(iv.width +15+5, (45-size.height)*0.5, size.width, size.height);
    [view addSubview:label];
    
    UIButton *myBtn = [[UIButton alloc] init];
    myBtn.frame = CGRectMake(ScreenWidth-55, (45-30)*0.5, 55, 30);
    myBtn.userInteractionEnabled = YES;
    [myBtn setTitle:detailTitle forState:UIControlStateNormal];
    [myBtn setTitleColor:[UIColor colorWithHexString:@"0880c6"] forState:UIControlStateNormal];
    myBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [myBtn addTarget:view action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:myBtn];


    for (int i = 0; i<texts.count; i++) {
        UIButton *btn = [[UIButton alloc]init];
        btn.tag = i;
        [btn addTarget:view action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:13];
        NSString *str = texts[i];
        [btn setTitle:str forState:UIControlStateNormal];
        CGSize strsize = [str sizeWithFont:[UIFont boldSystemFontOfSize:13]];
        
        btn.width = strsize.width + margin;
//        btn.height = strsize.height+ margin;
        btn.height = 27;
        if (i == 0) {
            btn.x = margin+5;
            btnW += CGRectGetMaxX(btn.frame);
        } else {
            btnW += CGRectGetMaxX(btn.frame)+margin;
            if (btnW > viewWidth) {
                count++;
                btn.x = margin+5;
                btnW = CGRectGetMaxX(btn.frame);
            } else{
                btn.x += btnW - btn.width;
            }
        }
        btn.backgroundColor = [UIColor whiteColor];//[[UIColor lightGrayColor]colorWithAlphaComponent:0.15f];
        btn.userInteractionEnabled = YES;
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [btn setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateSelected];
        btn.y += count * (btn.height + margin) + margin + iv.height + 15;
//        btn.y += count * (btn.height + margin) + margin + label.height +8;
        btn.layer.cornerRadius = 2;
        btn.clipsToBounds = YES;
        btn.layer.borderWidth = 0.5;
        btn.layer.borderColor = [UIColor colorWithHexString:@"e4e4e4"].CGColor;
        btn.tag = i;
        [view addSubview:btn];
    
        
        if (i == texts.count - 1) {
            view.height = CGRectGetMaxY(btn.frame) + 10;
            view.x = 0;
            view.width = viewWidth;
        }
    }
    
    if (texts.count == 0) {
        view.height = 0;
    }
    return view;
}

- (void)click1:(UIButton *)sender{
    NSLog(@"----");
    if ([self.Attribute_delegate respondsToSelector:@selector(rightBtnClick:)]) {
        [self.Attribute_delegate rightBtnClick:sender];
    }
    
}
- (void)btnClick:(UIButton *)sender{
    if (![self.btn isEqual:sender]) {
//        self.btn.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.15f];
        self.btn.selected = NO;
//        sender.backgroundColor = AppColor;
        sender.selected = YES;
        
        NSLog(@"title == %@", sender.titleLabel.text);
    }else if([self.btn isEqual:sender]){
        if (sender.selected == YES) {
//            sender.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.15f];
            sender.selected = NO;
        }else{
//            sender.backgroundColor = AppColor;
            sender.selected = YES;
        }
    }else{
        
    }
    if ([self.Attribute_delegate respondsToSelector:@selector(Attribute_View:didClickBtn:)] ) {
        [self.Attribute_delegate Attribute_View:self didClickBtn:sender];
    }
    self.btn = sender;
    
}

@end
